// @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
import {
	getLogin
} from "@/API/api.js"
import {
	AESKey
} from "./crypto.js";
export default function() {
	// 用户token登录
	const handleLogin = (callback) => {
		// const { authorization, appKey, routing, sn, channel, orderId,lang,classifyId,appScheme,isRoute,appVer } = route.query;
		// let urlPrams = authorization && appKey;
		// // 存储url参数，如果认证失效则重新登录
		// if (urlPrams) {
		// 	storage.clear()
		//     storage.set("VMSAuth", authorization);
		//     storage.set("VMSAPPKEY", appKey);
		// 	storage.set("VMSLANG", lang)
		// 	storage.set("VMSAPPSCHEME", appScheme)
		// 	storage.set("VMSAppVer", appVer)
		// 	if(routing){
		// 		storage.set('isRouting',routing)
		// 	}
		//     // storage.set('VMSUUID', sn)
		// }
		// const VMSLANG =  storage.get("VMSLANG");
		// locale.value = VMSLANG ? bindLang(VMSLANG) : bindLang(lang);//语言
		// storage.set('CHANNEL',channel);//存储通道号
		// 第一次进入界面 或 本地缓存不存在token，登录
		let tokenTime = uni.getStorageSync('VMSATOKENTIME') || 0
		let currentTime = new Date().getTime()
		if (!uni.getStorageSync('VMSATOKEN') || (tokenTime + 30 * 60 * 1000) < currentTime) {
			let params = {
				// authorization: authorization || storage.get("VMSAuth"),
				// appKey: appKey || storage.get("VMSAPPKEY"),
				// appScheme: appScheme || storage.get("VMSAPPSCHEME"),
				// appVer: appVer || storage.get("VMSAppVer")
				authorization: uni.getStorageSync('accessToken'),
				appKey: getApp().globalData.appConfig.xm_appKey,
				// appScheme: appScheme || storage.get("VMSAPPSCHEME"),
				// appVer: appVer || storage.get("VMSAppVer")
			}
			getLogin(params).then(res => {
				const {
					code,
					data
				} = res;
				if (code === 2000) {
					const {
						publicKey,
						accessToken
					} = data;
					uni.setStorageSync("VMSPUBLICKKEY", publicKey);
					uni.setStorageSync("VMSATOKEN", accessToken);
					uni.setStorageSync("VMSATOKENTIME", new Date().getTime());
					uni.setStorageSync("VMSKEY", AESKey());
					// router.push({ query: merge({}, { 'channel': channel, 'orderId': orderId,'sn':sn ,'classifyId':classifyId,'isRoute':isRoute}) })//isRoute表示是否从外部根据重定向routing参数跳转到购买界面
					callback()
				}
			}).catch(() => {})
		} else {
			callback();
		}
	}
	const httpReturn = (res, successCallback, failCallback) => {
		const {
			code,
			data
		} = res;
		if (code === 2000) {
			successCallback(data)
		} else {
			// 业务验证错误 ||  令牌错误  || 公共秘钥错误，请重新登录
			if (code === -91002 || code === -91003 || code === -91004) {
				// 待定
				return;
			}
			// if (code===-91003) {
			// 	uni.switchTab({
			// 		url: '/jvss/pages/cloud/cloud'
			// 	});
			// 	return;
			// }
			failCallback(code, data)
		}
	}
	return {
		// route,
		// router,
		handleLogin,
		httpReturn
	}
}
